package com.lulu.permissioncheckproject

import android.Manifest
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.widget.TextView

class MainActivity : AppCompatActivity() {
  val handler = Handler()
  private val runnable = object : Runnable {
    override fun run() {
      TestRequestPermission.tryGetPermission(this@MainActivity)
      handler.postDelayed(this, 5000)
    }
  }

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val findViewById = findViewById<TextView>(R.id.text)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      requestPermissions(arrayOf(
        Manifest.permission.READ_PHONE_STATE,
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION,
      ), 0)
    } else {
      handler.post(runnable)
    }
    findViewById.setOnClickListener {
      TestRequestPermission.tryGetPermission(this@MainActivity)
    }
  }

  override fun onRequestPermissionsResult(
    requestCode: Int,
    permissions: Array<out String>,
    grantResults: IntArray
  ) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    handler.post(runnable)
  }
}
